﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.Revit;
using QuickModel.Basic;
using QuickModel.CommonTrans;
using QuickModel.Forms;
using QuickModel.InteractiveOperation;
using QuickModel.LastFastModeling;
using QuickModel.Models;

namespace QuickModel.FastModelConvert
{
	public class FastModelConvertMaker : InteractiveOperationBase
	{
		public FastModelConvertMaker(ExternalCommandData useExternalCommandData) : base(useExternalCommandData)
		{
			this.useuiDoc = useExternalCommandData.Application.ActiveUIDocument;
		}

		public override List<InputRequest> GetAllInputRequest()
		{
			return this.m_lstRequestes;
		}

		public override bool IfBreak
		{
			get
			{
				return this.m_bIfBreak;
			}
		}

		public override bool IfEndCommand
		{
			get
			{
				return this.m_ifEndCommand;
			}
		}

		public override void CloseFrom()
		{
			try
			{
				bool flag = this.m_Frm != null;
				if (flag)
				{
					this.m_Frm.Close();
				}
			}
			catch
			{
			}
		}

		public FastModelConvertForm CreateInstrance(Autodesk.Revit.DB.Document doc, string speciatly, string equipType)
		{
			bool flag = this.m_Frm == null;
			if (flag)
			{
				this.m_Frm = new FastModelConvertForm(doc);
			}
			return this.m_Frm;
		}

		public override void PrepareRequest()
		{
			this.m_bIfBreak = true;
			this.m_bCancel = false;
			this.m_ifEndCommand = false;
			this.m_lstRequestes = new List<InputRequest>();
			Document document = this.useuiDoc.Document;
			bool flag = this.m_Frm == null;
			if (flag)
			{
				this.m_Frm = new FastModelConvertForm(document);
				this.m_Frm.Show(WindowHelper.HWndRevit);
			}
			WindowHelper.SendMessageToWndRevit();
			bool flag2 = this.m_Frm != null;
			if (flag2)
			{
				this.m_Frm.IsLeaveDlg = false;
			}
			List<ICadElement> list = new List<ICadElement>();
			FastModelConvertPara viewParas = this.m_Frm.GetViewParas();
			string publishGuid = this.m_Frm.PublishGuid;
			bool flag3 = viewParas == null || viewParas.IsWholeLevel;
			if (flag3)
			{
				bool flag4 = false;
				while (!flag4)
				{
					try
					{
						bool flag5 = !this.SelectBlocksByEquipType(viewParas, out this.lstTerminalBlockInfo);
						if (!flag5)
						{
							list.AddRange(this.lstTerminalBlockInfo);
							break;
						}
						flag4 = false;
						bool flag6 = false;
						string resultInfo = BussinessCommon.GetResultInfo(this.m_dwgEntitiesSelection, out flag6);
						bool flag7 = flag6;
						if (flag7)
						{
							this.m_ifEndCommand = true;
							this.m_bCancel = true;
							return;
						}
						YJKMessageBox.Show(resultInfo);
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
					{
						bool isLeaveDlg = this.m_Frm.IsLeaveDlg;
						if (isLeaveDlg)
						{
							this.m_bIfBreak = false;
						}
						throw ex;
					}
				}
			}
			else
			{
				bool isSelectBlock = this.m_IsSelectBlock;
				if (isSelectBlock)
				{
					bool flag8 = false;
					while (!flag8)
					{
						try
						{
							bool flag9 = !this.SelectBlocksByEquipType(viewParas, out this.lstTerminalBlockInfo);
							if (!flag9)
							{
								this.m_IsSelectBlock = false;
								break;
							}
							flag8 = false;
							bool flag10 = false;
							string resultInfo2 = BussinessCommon.GetResultInfo(this.m_dwgEntitiesSelection, out flag10);
							bool flag11 = flag10;
							if (flag11)
							{
								this.m_ifEndCommand = true;
								this.m_bCancel = true;
								return;
							}
							YJKMessageBox.Show(resultInfo2);
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException ex2)
						{
							this.m_IsSelectBlock = true;
							bool isLeaveDlg2 = this.m_Frm.IsLeaveDlg;
							if (isLeaveDlg2)
							{
								this.m_bIfBreak = false;
							}
							throw ex2;
						}
					}
				}
				List<ICadElement> list2 = null;
				try
				{
					this.m_dwgEntitiesSelection.TryGetEntitesInPickBox<ICadElement>(out list2, this.lstTerminalBlockInfo, new DelegateInRange<ICadElement>(this.CheckBlockIsInRange), "请框选需要转换的区域!");
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex3)
				{
					this.m_IsSelectBlock = true;
					this.m_bIfBreak = false;
					throw ex3;
				}
				catch
				{
				}
				bool flag12 = list2 != null && list2.Count > 0;
				if (flag12)
				{
					list.AddRange(list2);
				}
			}
			viewParas = this.m_Frm.GetViewParas();
			bool flag13 = publishGuid != this.m_Frm.PublishGuid;
			if (flag13)
			{
				this.m_IsSelectBlock = true;
				this.m_bCancel = true;
			}
			else
			{
				FamilySymbol docFamilySymbol = FastModelConvertMaker.GetDocFamilySymbol(document, viewParas.SelectedFamilyName, viewParas.SelectedModel);
				bool flag14 = docFamilySymbol == null;
				if (flag14)
				{
					YJKMessageBox.Show("获取族类型失败！");
					this.m_IsSelectBlock = true;
					this.m_bCancel = true;
				}
				else
				{
					List<ICadElement> list3 = new List<ICadElement>();
					foreach (ICadElement item in list)
					{
						list3.Add(item);
					}
					FastModelConvertInputRequest fastModelConvertInputRequest = new FastModelConvertInputRequest();
					fastModelConvertInputRequest.SelectDataMap.Add("SelectedBlocks", list3);
					fastModelConvertInputRequest.ViewParas = viewParas;
					fastModelConvertInputRequest.PlaceFs = docFamilySymbol;
					fastModelConvertInputRequest.m_Doc = document;
					this.m_lstRequestes.Add(fastModelConvertInputRequest);
				}
			}
		}

		private bool SelectBlocksByEquipType(FastModelConvertPara viewParas, out List<ICadElement> lstTerminalBlockInfo)
		{
			lstTerminalBlockInfo = new List<ICadElement>();
			string strPickGuidInfo = "请选择需要转换的图块！";
			List<PointPosBlockInfo> list = null;
			bool flag = this.m_dwgEntitiesSelection.TryPickSameNameBlockRef<PointPosBlockInfo>(out list, false, strPickGuidInfo, "", true);
			bool flag2 = !list.IsEmpty<PointPosBlockInfo>();
			if (flag2)
			{
				lstTerminalBlockInfo.AddRange(list);
			}
			bool flag3 = lstTerminalBlockInfo.IsEmpty<ICadElement>();
			return !flag3;
		}

		public bool CheckBlockIsInRange(ICadElement blockRef, PickedBox pickBox)
		{
			bool flag = blockRef == null || pickBox == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = blockRef is PointPosBlockInfo;
				if (flag2)
				{
					PointPosBlockInfo pointPosBlockInfo = blockRef as PointPosBlockInfo;
					XYZ useLocationPoint = pointPosBlockInfo.UseLocationPoint;
					bool flag3 = !BussinessCommon.GetBlockCenter(pointPosBlockInfo, out useLocationPoint);
					result = (!flag3 && RevitCommonFunctions.CheckPointIsInBox(pickBox, useLocationPoint));
				}
				else
				{
					result = false;
				}
			}
			return result;
		}

		public static FamilySymbol GetDocFamilySymbol(Autodesk.Revit.DB.Document doc, string familyName, string familySymbolName)
		{
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(FamilySymbol), doc);
			FamilySymbol familySymbol = null;
			bool flag = elementByClass.Count <= 0;
			FamilySymbol result;
			if (flag)
			{
				result = null;
			}
			else
			{
				foreach (Element element in elementByClass)
				{
					FamilySymbol familySymbol2 = element as FamilySymbol;
					bool flag2 = familySymbol2 == null;
					if (!flag2)
					{
						bool flag3 = familySymbol2.Name == familySymbolName && familySymbol2.Family.Name == familyName;
						if (flag3)
						{
							familySymbol = familySymbol2;
							break;
						}
					}
				}
				result = familySymbol;
			}
			return result;
		}

		private List<InputRequest> m_lstRequestes = new List<InputRequest>();

		private UIDocument useuiDoc;

		private bool m_bIfBreak = true;

		private bool m_ifEndCommand = false;

		private SpaceFamilyDataReader m_Sfdr = new SpaceFamilyDataReader();

		private FastModelConvertForm m_Frm = null;

		private bool m_IsSelectBlock = true;

		private List<ICadElement> lstTerminalBlockInfo = null;
	}
}
